package com.purchase.ml.service.dashboard.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.purchase.ml.bean.res.dashboard.*;
import com.purchase.ml.bean.vo.dealer.DealerVo;
import com.purchase.ml.bean.vo.order.OrderCurCollectVO;
import com.purchase.ml.consts.CommonConsts;
import com.purchase.ml.consts.Constants;
import com.purchase.ml.entity.acc.SysUser;
import com.purchase.ml.entity.dealer.SysDealer;
import com.purchase.ml.entity.order.SysDealerCollect;
import com.purchase.ml.mapper.dealer.SysDealerMapper;
import com.purchase.ml.mapper.order.SysDealerCollectMapper;
import com.purchase.ml.mapper.order.SysOrderMapper;
import com.purchase.ml.security.JwtUtil;
import com.purchase.ml.service.dashboard.DashboardService;
import com.purchase.ml.service.dealer.SysDealerService;
import com.purchase.ml.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.jwt.Jwt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by kyyang on 2021/3/4 22:23
 * 实时查询 则需要加入缓存
 */
@Slf4j
@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private SysDealerCollectMapper sysDealerCollectMapper;

    @Autowired
    private SysDealerMapper sysDealerMapper;

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysDealerService sysDealerService;


    @Override
    @Transactional
    public DashboardAdminRes queryAdminDashboard() {
        List<DealerVo> currentUserDear = sysDealerService.getCurrentUserDear();
        List<Long> dealers = currentUserDear.stream().map(e -> e.getDealerId()).collect(Collectors.toList());

        DashboardAdminRes dashboardAdminRes = new DashboardAdminRes();

        //此处查询parentId为持有者的汇总信息
        SysDealer sysDealer = sysDealerMapper.selectAgencyDate(CommonConsts.ROLETYPE_AGENCY);
        //计算昨天时间天
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,0);
        SimpleDateFormat sdf = DateUtils.dayFormat.get();
        String yesToday = sdf.format(calendar.getTime());
        //计算今天开始的时间；yesToday 近日开始；小于等于近日开始 就是昨天之前
        List<DashboardCurMonthOrderRes> day30Order = sysDealerCollectMapper.cur30DayDateMaster(yesToday,sysDealer.getDealerId());
        //设置30天范围的数据
        dashboardAdminRes.setCurMonthOrderNum(day30Order);

        //查询当天的数据
//        String today = sdf.format(new Date());
//        OrderCurCollectVO vo = new OrderCurCollectVO();
//        if(dealers.size() > 0) {
//            vo = sysOrderMapper.selectCurDayOrderCollect(today, dealers, JwtUtil.getCurrentUser().getRoleType());
//        }
//        if(vo == null){
//            vo = new OrderCurCollectVO();
//        }
        //汇总 总订单数
        DashboardConmonRes sumCollect = sysOrderMapper.sumPurchaseAgentOrMasterOrder(sysDealer.getDealerId());
        if(sumCollect ==  null){
            sumCollect = new DashboardConmonRes();
        }
        //查询出 经销商 主经销商数量 大代理 主经销商 经销商 对应 012
        long masterDealerNum = currentUserDear.stream().filter(e -> CommonConsts.ROLETYPE_MASTER  == e.getDealerType() && Constants.DEALER_STATE_REQ != e.getState() ).collect(Collectors.counting()).longValue();
        long dealerNum = currentUserDear.stream().filter(e -> CommonConsts.ROLETYPE_DEALER == e.getDealerType()  && Constants.DEALER_STATE_REQ != e.getState() ).collect(Collectors.counting()).longValue();
        //筛选待定的经销商做为未审核的
        long pendingDealerNum = currentUserDear.stream().filter(e -> Constants.DEALER_STATE_REQ == e.getState() && CommonConsts.ROLETYPE_DEALER == e.getDealerType()).collect(Collectors.counting()).longValue();
        dashboardAdminRes.setTotalPOs(sumCollect.getTotalPos());
        dashboardAdminRes.setTotalCompletedPos(sumCollect.getTotalCompletePos());
        dashboardAdminRes.setTotalProcessPOs(dashboardAdminRes.getTotalPOs() - dashboardAdminRes.getTotalCompletedPos());

        dashboardAdminRes.setTotalSales(sumCollect.getTotalSales());

        dashboardAdminRes.setTotalMasterDealers(masterDealerNum);
        dashboardAdminRes.setTotalDealers(dealerNum);
        dashboardAdminRes.setTotalPendingDealer(pendingDealerNum);


        return dashboardAdminRes;
    }

    @Override
    public DashboardAgentRes queryAgentDashboard() {
        SysUser sysUser = JwtUtil.getCurrentUser();
        List<DealerVo> currentUserDear = sysDealerService.getCurrentUserDear();
        List<Long> dealers = currentUserDear.stream().map(e -> e.getDealerId()).collect(Collectors.toList());
        DashboardAgentRes dashboardAgentRes = new DashboardAgentRes();

        //计算今天开始的时间；yesToday 近日开始；小于等于近日开始 就是昨天之前
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,0);
        SimpleDateFormat sdf = DateUtils.dayFormat.get();
        String yesToday = sdf.format(calendar.getTime());
        //查询近30天的数据
        List<DashboardCurMonthOrderRes> day30Order = sysDealerCollectMapper.cur30DayDateMaster(yesToday,sysUser.getDealerId());

        //设置30天范围的数据
        dashboardAgentRes.setCurMonthOrderNum(day30Order);


//        查询当天交易数据
//        String today = sdf.format(new Date());
//        OrderCurCollectVO vo = new OrderCurCollectVO();
//        if(dealers.size() > 0) {
//            vo = sysOrderMapper.selectCurDayOrderCollect(today, dealers, JwtUtil.getCurrentUser().getRoleType());
//        }
//        if(vo == null){
//            vo = new OrderCurCollectVO();
//        }

        //查询出 经销商 主经销商数量
        long masterDealerNum = currentUserDear.stream().filter(e -> CommonConsts.ROLETYPE_MASTER == e.getDealerType()).collect(Collectors.counting()).longValue();
        long pendingDealerNum = currentUserDear.stream().filter(e -> Constants.DEALER_STATE_REQ == e.getState() && CommonConsts.ROLETYPE_DEALER == e.getDealerType() && e.getIsAuth() == 1 ).collect(Collectors.counting()).longValue();

        dashboardAgentRes.setTotalMasterDealers(masterDealerNum); //主经销商数量
        dashboardAgentRes.setTotalPendingDealer(pendingDealerNum);//待完成的经销商数量

        //汇总 总订单数
        DashboardConmonRes sumCollect = sysOrderMapper.sumPurchaseAgentOrMasterOrder(sysUser.getDealerId());
        if(sumCollect ==  null){
            sumCollect = new DashboardConmonRes();
        }
        dashboardAgentRes.setTotalPOs(sumCollect.getTotalPos());
        dashboardAgentRes.setTotalCompletedPos(sumCollect.getTotalCompletePos());
        dashboardAgentRes.setTotalProcessPOs(sumCollect.getTotalPos() - sumCollect.getTotalCompletePos());

        return dashboardAgentRes;
    }

    @Override
    public DashboardMasterDealerRes queryMasterDealerDashboard() {
        //获取用户
        SysUser sysUser = JwtUtil.getCurrentUser();
        List<DealerVo> currentUserDear = sysDealerService.getCurrentUserDear();
        log.info("currentUserDear======{}",currentUserDear);
        log.info("sysUser==========={}",sysUser);
        List<Long> dealers = currentUserDear.stream().map(e -> e.getDealerId()).collect(Collectors.toList());
        DashboardMasterDealerRes dashboardMasterDealerRes = new DashboardMasterDealerRes();
        //计算今天开始的时间；yesToday 近日开始；小于等于近日开始 就是昨天之前
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,0);
        SimpleDateFormat sdf = DateUtils.dayFormat.get();
        String yesToday = sdf.format(calendar.getTime());
        //查询近30天的数据
        List<DashboardCurMonthOrderRes> day30Order = sysDealerCollectMapper.cur30DayDateMaster(yesToday,sysUser.getDealerId());
        //设置30天范围的数据
        dashboardMasterDealerRes.setCurMonthOrderNum(day30Order);

        //查询当天交易数据
//        String today = sdf.format(new Date());
//        log.info("today====={}",today);
//        OrderCurCollectVO vo = new OrderCurCollectVO();
//        if(dealers.size() > 0){
//           vo = sysOrderMapper.selectCurDayOrderCollect(today, dealers, JwtUtil.getCurrentUser().getRoleType());
//        }
//        if(vo == null){
//            vo = new OrderCurCollectVO();
//        }
//        log.info("sumCollect==1111==={}",vo);
        //查询出 经销商数量
        long dealerNum = currentUserDear.stream().filter(e -> CommonConsts.ROLETYPE_DEALER == e.getDealerType()).collect(Collectors.counting()).longValue();
        long pendingDealerNum = currentUserDear.stream().filter(e -> Constants.DEALER_STATE_REQ == e.getState() && e.getIsAuth()==0).collect(Collectors.counting()).longValue();
        dashboardMasterDealerRes.setTotalDealers(dealerNum);
        dashboardMasterDealerRes.setTotalPendingDealer(pendingDealerNum);

        //汇总 总订单数
        DashboardConmonRes sumCollect = sysOrderMapper.sumPurchaseAgentOrMasterOrder(sysUser.getDealerId());
        if(sumCollect ==  null){
            sumCollect = new DashboardConmonRes();
        }
        log.info("sumCollect====={}",sumCollect);
        //设置订单数量
        dashboardMasterDealerRes.setTotalPOs(sumCollect.getTotalPos());
        dashboardMasterDealerRes.setTotalCompletedPos(sumCollect.getTotalCompletePos());
        dashboardMasterDealerRes.setTotalProcessPOs(sumCollect.getTotalPos()-sumCollect.getTotalCompletePos());
        return dashboardMasterDealerRes;
    }

    @Override
    public DashboardDealerRes queryDealerDashboard() {
        SysUser sysUser = JwtUtil.getCurrentUser();
        DashboardDealerRes dashboardDealerRes = new DashboardDealerRes();
        //计算昨天时间天
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,0);
        SimpleDateFormat sdf = DateUtils.dayFormat.get();
        String yesTodayEnd = sdf.format(calendar.getTime());
        //查询近30天的数据
        List<DashboardCurMonthOrderRes> day30Order = sysDealerCollectMapper.cur30DayDateDealer(yesTodayEnd,sysUser.getDealerId());
        //设置30天范围的数据
        dashboardDealerRes.setCurMonthOrderNum(day30Order);

        //查询当天交易数据
//        String today = sdf.format(new Date());
//        OrderCurCollectVO vo = new OrderCurCollectVO();
//        if(dealers.size() > 0) {
//           vo = sysOrderMapper.selectCurDayOrderCollect(today, dealers, JwtUtil.getCurrentUser().getRoleType());
//        }
//        if(vo == null){
//            vo = new OrderCurCollectVO();
//        }

        //汇总 总订单数
        DashboardConmonRes sumCollect = sysOrderMapper.sumPurchaseDealerOrder(sysUser.getDealerId());
        if(sumCollect ==  null){
            sumCollect = new DashboardConmonRes();
        }
        //设置订单数量
        dashboardDealerRes.setTotalPOs(sumCollect.getTotalPos());
        dashboardDealerRes.setTotalCompletedPos(sumCollect.getTotalCompletePos());
        dashboardDealerRes.setTotalProcessPOs(dashboardDealerRes.getTotalPOs() - dashboardDealerRes.getTotalCompletedPos());

        return dashboardDealerRes;
    }
}
